﻿using Bandit.Helpers;
using ETC.DLEDPositionTool.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETC.DLEDPositionTool.Models
{
    public class TestInfo : Bandit.UI.MVVM.NotifyObject
    {
        #region Pass
        private int _Pass;
        /// <summary>
        /// Pass Count
        /// </summary>
        public int Pass
        {
            get { return _Pass; }
            set
            {
                _Pass = value;
                OnPropertyChanged("Pass");
            }
        }
        #endregion

        #region Fail
        private int _Fail;
        /// <summary>
        /// Fail Count
        /// </summary>
        public int Fail
        {
            get { return _Fail; }
            set
            {
                _Fail = value;
                OnPropertyChanged("Fail");
            }
        }
        #endregion

        #region Total
        private int _Total;
        /// <summary>
        /// Total Count
        /// </summary>
        public int Total
        {
            get { return _Total; }
            set
            {
                _Total = value;
                OnPropertyChanged("Total");
            }
        }
        #endregion

        #region Barcode
        private string _Barcode;
        /// <summary>
        /// Barcode
        /// </summary>
        public string Barcode
        {
            get { return _Barcode; }
            set
            {
                _Barcode = value;
                OnPropertyChanged("Barcode");
            }
        }
        #endregion

        #region BarcodeIsReadOnly

        private bool _BarcodeIsReadOnly;
        /// <summary>
        /// Barcode文本框是否只读
        /// </summary>
        public bool BarcodeIsReadOnly
        {
            get { return _BarcodeIsReadOnly; }
            set
            {
                _BarcodeIsReadOnly = value;
                OnPropertyChanged("BarcodeIsReadOnly");
            }
        }

        #endregion

        #region Result
        /// <summary>
        /// Result
        /// </summary>
        public TestResultState Result { get; private set; }

        /// <summary>
        /// Result Background
        /// </summary>
        public string ResultBackground { get; private set; }

        /// <summary>
        /// Result Icon
        /// </summary>
        public string ResultIcon { get; private set; }

        private System.Windows.Visibility _ResultVisibility;
        /// <summary>
        /// 结果显示状态
        /// </summary>
        public System.Windows.Visibility ResultVisibility
        {
            get { return _ResultVisibility; }
            set
            {
                _ResultVisibility = value;
                OnPropertyChanged("ResultVisibility");
            }
        }
        #endregion

        #region 进度
        private double _Progress;
        /// <summary>
        /// 测试进度
        /// </summary>
        public double Progress
        {
            get { return _Progress; }
            set
            {
                if (value == _Progress)
                {
                    return;
                }
                _Progress = value;
                OnPropertyChanged("Progress");
            }
        }

        private System.Windows.Visibility _ProgressBarVisibility;
        /// <summary>
        /// 进度条显示状态
        /// </summary>
        public System.Windows.Visibility ProgressBarVisibility
        {
            get { return _ProgressBarVisibility; }
            set
            {
                _ProgressBarVisibility = value;
                OnPropertyChanged("ProgressBarVisibility");
            }
        }
        #endregion

        #region 计时
        private string _TestTime;
        /// <summary>
        /// 测试时间
        /// </summary>
        public string TestTime
        {
            get { return _TestTime; }
            set
            {
                _TestTime = value;
                OnPropertyChanged("TestTime");
            }
        }

        public Bandit.Helpers.TestTimer TestTimer { get; private set; }
        public void Startwatch()
        {
            System.Threading.Tasks.Task.Factory.StartNew((Action)delegate
            {
                TestTimer.Start();

                while (TestTimer.IsRunning)
                {
                    this.TestTime = TestTimer.TestTime;

                    System.Threading.Thread.Sleep(5);
                }

                this.TestTime = TestTimer.TestTime;
            });
        }

        public void Stopwatch()
        {
            TestTimer.Stop();
        }
        #endregion

        #region EnabledStartButton
        private bool _EnabledStartButton;
        /// <summary>
        /// 启用或禁用开始按钮
        /// </summary>
        public bool EnabledStartButton
        {
            get { return _EnabledStartButton; }
            set
            {
                _EnabledStartButton = value;
                OnPropertyChanged("EnabledStartButton");
            }
        }
        #endregion

        #region IsTesting
        private bool _IsTesting;
        /// <summary>
        /// 是否测试中
        /// </summary>
        public bool IsTesting
        {
            get { return _IsTesting; }
            set
            {
                _IsTesting = value;
                OnPropertyChanged("IsTesting");
            }
        }
        #endregion


        #region TestCodeModels
        private List<TestCodeModel> _TestCodeModels;

        public List<TestCodeModel> TestCodeModels { get { return _TestCodeModels; } set { _TestCodeModels = value; OnPropertyChanged(nameof(TestCodeModels)); } }

        #endregion

        #region 测试指令

        private int _TestModelCode;

        public int TestModelCode { get { return _TestModelCode; } set { _TestModelCode = value; OnPropertyChanged(nameof(TestModelCode)); } }

        private bool _IsVLedSet = true;
        public bool IsVLedSet { get { return _IsVLedSet; } set { _IsVLedSet = value; OnPropertyChanged(nameof(IsVLedSet)); } }

        private double _DvledValue = 6.72;

        public double DvledValue { get { return _DvledValue; } set { _DvledValue = value; OnPropertyChanged(nameof(DvledValue)); } }


        private bool _IsOCPOVPSet = true;
        public bool IsOCPOVPSet { get { return _IsOCPOVPSet; } set { _IsOCPOVPSet = value; OnPropertyChanged(nameof(IsOCPOVPSet)); } }

        private double _DvLedOvpValue = 8;
        public double DvLedOvpValue { get { return _DvLedOvpValue; } set { _DvLedOvpValue = value; OnPropertyChanged(nameof(DvLedOvpValue)); } }

        private double _DvLedOcpValue = 4;
        public double DvLedOcpValue { get { return _DvLedOcpValue; } set { _DvLedOcpValue = value; OnPropertyChanged(nameof(DvLedOcpValue)); } }

        private double _DICOvpValue = 3.4;
        public double DICOvpValue { get { return _DICOvpValue; } set { _DICOvpValue = value; OnPropertyChanged(nameof(DICOvpValue)); } }

        private double _DICOcpValue = 0.2;
        public double DICOcpValue { get { return _DICOcpValue; } set { _DICOcpValue = value; OnPropertyChanged(nameof(DICOcpValue)); } }

        private bool _IsMCSet = true;

        public bool IsMCSet { get { return _IsMCSet; } set { _IsMCSet = value; OnPropertyChanged(nameof(IsMCSet)); } }

        private int _IMCValue = 1;

        public int IMCValue { get { return _IMCValue; } set { _IMCValue = value; OnPropertyChanged(nameof(IMCValue)); } }

        private bool _IsDCSet = true;

        public bool IsDCSet { get { return _IsDCSet; } set { _IsDCSet = value; OnPropertyChanged(nameof(IsDCSet)); } }

        private int _IDCValue = 1;

        public int IDCValue { get { return _IDCValue; } set { _IDCValue = value; OnPropertyChanged(nameof(IDCValue)); } }

        private bool _IsBCSet = true;

        public bool IsBCSet { get { return _IsBCSet; } set { _IsBCSet = value; OnPropertyChanged(nameof(IsBCSet)); } }

        private int _IBCValue = 1;

        public int IBCValue { get { return _IBCValue; } set { _IBCValue = value; OnPropertyChanged(nameof(IBCValue)); } }

        private bool _IsScaleSet = true;
        public bool IsScaleSet { get { return _IsScaleSet; } set { _IsScaleSet = value; OnPropertyChanged(nameof(IsScaleSet)); } }

        private int _IScaleValue = 1;

        public int IScaleValue { get { return _IScaleValue; } set { _IScaleValue = value; OnPropertyChanged(nameof(IScaleValue)); } }

        private bool _IsSRCSet = true;

        public bool IsSRCSet { get { return _IsSRCSet; } set { _IsSRCSet = value; OnPropertyChanged(nameof(IsSRCSet)); } }

        private int _ISrcValue = 1;

        public int ISrcValue { get { return _ISrcValue; } set { _ISrcValue = value; OnPropertyChanged(nameof(ISrcValue)); } }

        private bool _IsLEDNUMSet = true;

        public bool IsLEDNUMSet { get { return _IsLEDNUMSet; } set { _IsLEDNUMSet = value; OnPropertyChanged(nameof(IsLEDNUMSet)); } }

        private int _ILEDNUMValue = 1;

        public int ILEDNUMValue { get { return _ILEDNUMValue; } set { _ILEDNUMValue = value; OnPropertyChanged(nameof(ILEDNUMValue)); } }


        private bool _IsRSETSet = true;

        public bool IsRSETSet { get { return _IsRSETSet; } set { _IsRSETSet = value; OnPropertyChanged(nameof(IsRSETSet)); } }

        private int _IRSETValue = 1;

        public int IRSETValue { get { return _IRSETValue; } set { _IRSETValue = value;  OnPropertyChanged(nameof(IRSETValue)); } }


        private bool _IsCLKSet = true;

        public bool IsCLKSet { get { return _IsCLKSet; } set { _IsCLKSet = value; OnPropertyChanged(nameof(IsCLKSet)); } }

        private int _ICLKValue = 1;

        public int ICLKValue { get { return _ICLKValue; } set { _ICLKValue = value; OnPropertyChanged(nameof(ICLKValue)); } }

        private bool _IsILEDSet = true;

        public bool IsILEDSet { get { return _IsILEDSet; } set { _IsILEDSet = value; OnPropertyChanged(nameof(IsILEDSet)); } }

        private int _IILEDValue = 1;

        public int IILEDValue { get { return _IILEDValue; } set { _IILEDValue = value; OnPropertyChanged(nameof(IILEDValue)); } }

        private bool _IsGCG1Set = true;

        public bool IsGCG1Set { get { return _IsGCG1Set; } set { _IsGCG1Set = value; OnPropertyChanged(nameof(IsGCG1Set)); } }

        private int _IGCG1Value = 1;

        public int IGCG1Value { get { return _IGCG1Value; } set { _IGCG1Value = value; OnPropertyChanged(nameof(IGCG1Value)); } }


        private bool _IsGCG2Set = true;

        public bool IsGCG2Set { get { return _IsGCG2Set; } set { _IsGCG2Set = value; OnPropertyChanged(nameof(IsGCG2Set)); } }

        private int _IGCG2Value = 1;

        public int IGCG2Value { get { return _IGCG2Value; } set { _IGCG2Value = value; OnPropertyChanged(nameof(IGCG2Value)); } }

        private bool _IsHLMSSet = true;

        public bool IsHLMSSet { get { return _IsHLMSSet; } set { _IsHLMSSet = value; OnPropertyChanged(nameof(IsHLMSSet)); } }

        private int _IHLMSValue = 1;

        public int IHLMSValue { get { return _IHLMSValue; } set { _IHLMSValue = value; OnPropertyChanged(nameof(IHLMSValue)); } }

        private double _IoutCurrentValue;

        public double IoutCurrentValue
        {
            get { return _IoutCurrentValue; }
            set
            {
                _IoutCurrentValue = value;

                OnPropertyChanged(nameof(IoutCurrentValue));
            }
        }

        #region 点灯机点灯参数

        private int _SetMCValue;

        public int SetMCValue { get { return _SetMCValue; } set { _SetMCValue = value; OnPropertyChanged(nameof(SetMCValue)); } }

        private int _SetDCValue;

        public int SetDCValue { get { return _SetDCValue; } set { _SetDCValue = value; OnPropertyChanged(nameof(SetDCValue)); } }

        private int _SetBCValue;

        public int SetBCValue { get { return _SetBCValue; } set { _SetBCValue = value; OnPropertyChanged(nameof(SetBCValue)); } }

        private int _SetScaleValue;

        public int SetScaleValue { get { return _SetScaleValue; } set { _SetScaleValue = value; OnPropertyChanged(nameof(SetScaleValue)); } }

        private int _SetIledValue;
        public int SetIledValue { get { return _SetIledValue; } set { _SetIledValue = value; OnPropertyChanged(nameof(SetIledValue)); } }

        private int _SetSRCValue;

        public int SetSRCValue { get { return _SetSRCValue; } set { _SetSRCValue = value; OnPropertyChanged(nameof(SetSRCValue)); } }

       

      
        private int _SetGCG1Value = 0;

        public int SetGCG1Value { get { return _SetGCG1Value; } set { _SetGCG1Value = value; OnPropertyChanged(nameof(SetGCG1Value)); } }

        private int _SetGCG2Value = 0;

        public int SetGCG2Value { get { return _SetGCG2Value; } set { _SetGCG2Value = value; OnPropertyChanged(nameof(SetGCG2Value)); } }

        private int _SetHLMSValue = 0;

        public int SetHLMSValue { get { return _SetHLMSValue; } set { _SetHLMSValue = value; OnPropertyChanged(nameof(SetHLMSValue)); } }


        private int _SetForLightValue;

        public int SetForLightValue { get { return _SetForLightValue; } set { _SetForLightValue = value; OnPropertyChanged(nameof(SetForLightValue)); } }

        private int _SetBackLightValue;

        public int SetBackLightValue { get { return _SetBackLightValue; } set { _SetBackLightValue = value; OnPropertyChanged(nameof(SetBackLightValue)); } }

        private int _SetLocationICValue;

        public int SetLocationICValue { get { return _SetLocationICValue; } set { _SetLocationICValue = value; OnPropertyChanged(nameof(SetLocationICValue)); } }

        private int _SetLocationLedValue;

        public int SetLocationLedValue { get { return _SetLocationLedValue; } set { _SetLocationLedValue = value; OnPropertyChanged(nameof(SetLocationLedValue)); } }
        private int _SetLocationScanNumValue;

        public int SetLocationScanNumValue { get { return _SetLocationScanNumValue; } set { _SetLocationScanNumValue = value; OnPropertyChanged(nameof(SetLocationScanNumValue)); } }
        #endregion

        #region 面积
        private int _MaxAreaValue;

        public int MaxAreaValue
        {

            get { return _MaxAreaValue; }

            set
            {
                _MaxAreaValue = value;


                OnPropertyChanged(nameof(MaxAreaValue));
            }
        }
        private int _MinAreaValue;

        public int MinAreaValue
        {

            get { return _MinAreaValue; }

            set
            {
                _MinAreaValue = value;


                OnPropertyChanged(nameof(MinAreaValue));
            }
        }

        private int _ThreasholdValue;

        public int ThreasholdValue
        {
            get { return _ThreasholdValue; }
            set
            {

                _ThreasholdValue = value;
                OnPropertyChanged(nameof(ThreasholdValue));
            }
        }
        #endregion

        #endregion

        public TestInfo()
        {
            this.Reset();
        }

        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            this._Pass = 0;
            this._Fail = 0;
            this._Total = 0;
            this.UpdateResult(TestResultState.Idle);
            this._ResultVisibility = System.Windows.Visibility.Visible;
            this._Progress = 0.0;
            this._ProgressBarVisibility = System.Windows.Visibility.Collapsed;
            this._TestTime = "00:00:00.000";
            this.TestTimer = new Bandit.Helpers.TestTimer();

            this._TestCodeModels = new List<TestCodeModel>();
            string CodeModelStr = ConfigHelper.GetVauleToString("TestCodeModels");

            string[] CodeModelArr = CodeModelStr.Split(';');

            for (int i = 0; i < CodeModelArr.Length; i++)
            {
                string[] CodeArr = CodeModelArr[i].Split(":");

                this._TestCodeModels.Add(new TestCodeModel(CodeArr[1], int.Parse(CodeArr[0])));
            }
        }


        #region 更新结果
        private void UpdateResult(TestResultState result)
        {
            switch (result)
            {
                case TestResultState.Idle:
                    this.ResultBackground = MyBrushes.IDLE.ToBrushString();
                    this.ResultIcon = "Images/IDLE.png";
                    break;
                case TestResultState.Testing:
                    this.ResultBackground = MyBrushes.TESTING.ToBrushString();
                    this.ResultIcon = "Images/Testing.png";
                    break;
                case TestResultState.Pass:
                    this.Pass++;
                    this.Total++;
                    this.ResultBackground = MyBrushes.PASS.ToBrushString();
                    this.ResultIcon = "Images/Pass.png";
                    break;
                case TestResultState.Fail:
                    this.Fail++;
                    this.Total++;
                    this.ResultBackground = MyBrushes.FAIL.ToBrushString();
                    this.ResultIcon = "Images/Fail.png";
                    break;
                case TestResultState.Stop:
                    this.ResultBackground = MyBrushes.RichYellow.ToBrushString();
                    this.ResultIcon = "Images/Fail.png";
                    break;
                default:
                    break;
            }
            this.Result = result;
            OnPropertyChanged("Result");
            OnPropertyChanged("ResultBackground");
            OnPropertyChanged("ResultIcon");
        }
        #endregion

        private void StartProgress()
        {
            this.ResultVisibility = System.Windows.Visibility.Collapsed;
            this.Progress = 0.0;
            this.ProgressBarVisibility = System.Windows.Visibility.Visible;
        }

        private void EndProgress()
        {
            this.ProgressBarVisibility = System.Windows.Visibility.Collapsed;
            this.ResultVisibility = System.Windows.Visibility.Visible;
        }

        public void TestStart()
        {
            this.EnabledStartButton = false;
            this.IsTesting = true;
            this.StartProgress();
            this.UpdateResult(TestResultState.Testing);
            this.Startwatch();
            this.BarcodeIsReadOnly = true;
        }

        public void TestEnd(TestResultState result)
        {
            this.Stopwatch();
            this.EnabledStartButton = true;
            this.IsTesting = false;
            this.UpdateResult(result);
            this.EndProgress();
            this.BarcodeIsReadOnly = false;
        }
    }
}
